package es.cazadoresmadridejos.beans.dao.biggame.impl;

import java.sql.SQLException;
import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import es.cazadoresmadridejos.beans.dao.biggame.BigGameDao;
import es.cazadoresmadridejos.common.bean.dao.impl.DaoImpl;
import es.cazadoresmadridejos.model.BigGame;
import es.cazadoresmadridejos.model.BigGame.BigGameStatus;
import es.cazadoresmadridejos.objs.web.Page;
import es.cazadoresmadridejos.objs.web.PaginatedListImpl;

@Repository("bigGameDao")
public class BigGameDaoImpl extends DaoImpl<BigGame> implements BigGameDao{

	@Override
	public BigGame newInstance() {
		return new BigGame();
	}

	@Override
	public PaginatedList findAllPaginated(final Page page, final Long year) {
		return (PaginatedList) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				StringBuilder hqlCount = new StringBuilder();
				hql
				.append("select b ")
				.append(" from BigGame b ")
				.append(" where b.reason.id = :idReason ")
				.append(getSort(page, "b"));

				hqlCount
				.append("select count(b) ")
				.append(" from BigGame b ")
				.append(" where b.reason.id = :idReason ");
				
				Query query = session.createQuery(hql.toString());
				Query queryCount = session.createQuery(hqlCount.toString());
			
				query.setLong("idReason", year);
				queryCount.setLong("idReason", year);
				
				return new PaginatedListImpl<Object>(query.list(), 
						page, 
						((Long)queryCount.uniqueResult()).intValue());
			}
		});
	}

	@Override
	public BigGame getInProgressBigGame() {
		return (BigGame) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
				.append("select bg ")
				.append(" from BigGame bg")
				.append(" where bg.status = :status ")
				.append("");

				Query query = session.createQuery(hql.toString());
				query.setParameter("status", BigGameStatus.EN_PROCESO);
				
				return query.uniqueResult();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BigGame> findAll(final Long reasonId) {
		return (List<BigGame>)getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql.append("select r ").append(" from BigGame r where r.reason.id = :reasonId");

				Query query = session.createQuery(hql.toString());
				query.setLong("reasonId", reasonId);
				
				return query.list();
			}
		});
	}

}
